package com.example.firstapp.core;

import java.util.List;

import org.apache.http.client.UserTokenHandler;

import com.example.firstapp.entity.Restaurant;
import com.example.firstapp.entity.Domain.FoodType;
import com.example.firstapp.entity.Domain.PriceType;
import com.example.firstapp.entity.UserPreference;

public class PreferenceContext implements UserContext {

	UserPreference userPref;

	public PreferenceContext(UserPreference userPref) {
		super();
		this.userPref = userPref;
	}

	@Override
	public double evaluate(Restaurant restaurant) {
		// TODO Auto-generated method
		boolean foodTypeMatch = false;
		double totalGrade = 0.0;
		double foodTypeGrade = 0.0;
		double priceGrade = 0.0;
		double buyGoGrade = 0.0;
		List<FoodType> foodTypeRes = restaurant.getFoodtypes();
		PriceType priceRes = restaurant.getPriceType();
		for (FoodType foodtype : userPref.getFoodtypes()) {
			for (FoodType foodtyperes : foodTypeRes) {
				if (foodtype == foodtyperes) {
					foodTypeMatch = true;
					foodTypeGrade = 10;
					break;
				}
			}
			if (foodTypeMatch)
				break;
		}
		if (userPref.getPriceType().equals(priceRes)) {
			priceGrade = 10.0;
		} else {
			if (userPref.getPriceType().equals(PriceType.MEDIUM))
				priceGrade = 6.0;
			else if (priceRes.equals(PriceType.MEDIUM))
				priceGrade = 6.0;
			else
				priceGrade = 2.0;
		}
		if (userPref.getBuyToGo() && !restaurant.isTakeAway())
			buyGoGrade = 0.0;
		else
			buyGoGrade = 10.0;
		totalGrade = (userPref.getFoodTypeWeight() * foodTypeGrade
				+ userPref.getPriceTypeWeight() * priceGrade + userPref
				.getBuyToGoWeight() * buyGoGrade)
				/ (userPref.getFoodTypeWeight() + userPref.getPriceTypeWeight() + userPref
						.getBuyToGoWeight());
		return totalGrade;
	}
}
